package com.imooc.service.impl.center;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageHelper;
import com.imooc.enums.OrderStatusEnum;
import com.imooc.enums.YesOrNo;
import com.imooc.mapper.OrderStatusMapper;
import com.imooc.mapper.OrdersMapper;
import com.imooc.mapper.OrdersMapperCustom;
import com.imooc.pojo.OrderStatus;
import com.imooc.pojo.Orders;
import com.imooc.pojo.vo.MyOrdersVO;
import com.imooc.pojo.vo.OrderStatusCountsVO;
import com.imooc.service.center.MyOrdersService;
import com.imooc.utils.PagedGridResult;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
@AllArgsConstructor
public class MyOrdersServiceImpl extends BaseService implements MyOrdersService {


  private final OrdersMapperCustom ordersMapperCustom;

  private final OrdersMapper ordersMapper;

  private final OrderStatusMapper orderStatusMapper;

  @Transactional(rollbackFor = Exception.class, propagation = Propagation.SUPPORTS)
  @Override
  public PagedGridResult queryMyOrders(String userId,
      Integer orderStatus,
      Integer page,
      Integer pageSize) {
    Map<String, Object> map = new HashMap<>();
    map.put("userId", userId);
    if (orderStatus != null) {
      map.put("orderStatus", orderStatus);
    }
    PageHelper.startPage(page, pageSize);
    List<MyOrdersVO> list = ordersMapperCustom.queryMyOrders(map);
    return setterPagedGrid(list, page);
  }

//    private PagedGridResult setterPagedGrid(List<?> list, Integer page) {
//        PageInfo<?> pageList = new PageInfo<>(list);
//        PagedGridResult grid = new PagedGridResult();
//        grid.setPage(page);
//        grid.setRows(list);
//        grid.setTotal(pageList.getPages());
//        grid.setRecords(pageList.getTotal());
//        return grid;
//    }

  @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
  @Override
  public void updateDeliverOrderStatus(String orderId) {

    OrderStatus updateOrder = new OrderStatus();
    updateOrder.setOrderStatus(OrderStatusEnum.WAIT_RECEIVE.type);
    updateOrder.setDeliverTime(new Date());
    orderStatusMapper.update(updateOrder, Wrappers.<OrderStatus>lambdaQuery()
        .eq(OrderStatus::getOrderId, orderId)
        .eq(OrderStatus::getOrderStatus, OrderStatusEnum.WAIT_DELIVER.type));
  }

  @Transactional(rollbackFor = Exception.class, propagation = Propagation.SUPPORTS)
  @Override
  public Orders queryMyOrder(String userId, String orderId) {
    return ordersMapper.selectOne(Wrappers.<Orders>lambdaQuery()
        .eq(Orders::getUserId, userId)
        .eq(Orders::getId, orderId)
        .eq(Orders::getIsDelete, YesOrNo.NO.type));
  }

  @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
  @Override
  public boolean updateReceiveOrderStatus(String orderId) {
    OrderStatus updateOrder = new OrderStatus();
    updateOrder.setOrderStatus(OrderStatusEnum.SUCCESS.type);
    updateOrder.setSuccessTime(new Date());
    int result = orderStatusMapper
        .update(updateOrder, Wrappers.<OrderStatus>lambdaQuery()
            .eq(OrderStatus::getOrderId, orderId)
            .eq(OrderStatus::getOrderStatus, OrderStatusEnum.WAIT_RECEIVE.type));
    return result == 1;
  }

  @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
  @Override
  public boolean deleteOrder(String userId, String orderId) {
    Orders updateOrder = new Orders();
    updateOrder.setIsDelete(YesOrNo.YES.type);
    updateOrder.setUpdatedTime(new Date());
    int result = ordersMapper.update(updateOrder, Wrappers.<Orders>lambdaQuery()
        .eq(Orders::getId, orderId)
        .eq(Orders::getUserId, userId));
    return result == 1;
  }

  @Transactional(rollbackFor = Exception.class, propagation = Propagation.SUPPORTS)
  @Override
  public OrderStatusCountsVO getOrderStatusCounts(String userId) {

    Map<String, Object> map = new HashMap<>();
    map.put("userId", userId);

    map.put("orderStatus", OrderStatusEnum.WAIT_PAY.type);
    int waitPayCounts = ordersMapperCustom.getMyOrderStatusCounts(map);

    map.put("orderStatus", OrderStatusEnum.WAIT_DELIVER.type);
    int waitDeliverCounts = ordersMapperCustom.getMyOrderStatusCounts(map);

    map.put("orderStatus", OrderStatusEnum.WAIT_RECEIVE.type);
    int waitReceiveCounts = ordersMapperCustom.getMyOrderStatusCounts(map);

    map.put("orderStatus", OrderStatusEnum.SUCCESS.type);
    map.put("isComment", YesOrNo.NO.type);
    int waitCommentCounts = ordersMapperCustom.getMyOrderStatusCounts(map);

    OrderStatusCountsVO countsVO = new OrderStatusCountsVO(waitPayCounts,
        waitDeliverCounts,
        waitReceiveCounts,
        waitCommentCounts);
    return countsVO;
  }

  @Transactional(rollbackFor = Exception.class, propagation = Propagation.SUPPORTS)
  @Override
  public PagedGridResult getOrdersTrend(String userId, Integer page, Integer pageSize) {
    Map<String, Object> map = new HashMap<>();
    map.put("userId", userId);
    PageHelper.startPage(page, pageSize);
    List<OrderStatus> list = ordersMapperCustom.getMyOrderTrend(map);
    return setterPagedGrid(list, page);
  }
}
